layui.define(['config', 'layer'], function (exports) {
    var config = layui.config;
    var layer = layui.layer;
    var up = {};
    up.defaultParams = {
        url: config.base_server + "/cms/cms-file/files-anon",
       // imageBaseUrl: config.spath,//上传图片路径的前缀
        elem: "#test1",
        accept: "images",
        acceptMime: "",
        exts: "",
        type:"POST",
        size: 2048, //上传大小
        width: 50,   //上传宽度 大于0时生效
        height: 50,   //上传高度
        scale: [], //宽和高比例 [2,1] 必须大于0  计算顺序在 width-height-scale
        multiple: false,
        choose: null,
        auto: true,
        before: function (obj) { //obj参数包含的信息，跟 choose回调完全一致，可参见上文。
            layer.load(); //上传loading
        },
        error: function (index, upload) {
            layer.closeAll('loading'); //关闭loading
        }
    }
    up.uploadFile = function (params) {
        if (!params) {
            params = {};
        }
        if (!params.accept || "images" == params.accept) { //默认为 images 图片时 高宽才可以生效
            if (0 < params.width || 0 < params.height || (params.scale && params.scale.length == 2)) {//高或者宽 大于0 时 或者设置了比例   设置为不自动提交 并设置手动提交方法
                params.auto = false;
                params.choose = function (obj) {
                    //将每次选择的文件追加到文件队列
                    var files = obj.pushFile();
                    obj.preview(function (index, file, result) {
                        var img = new Image();
                        img.onload = function () {
                            console.log('choose poster', img.width, img.height);
                            if (0 < params.width && params.width != img.width) {
                                layer.msg('图片宽度必须为' + params.width + 'px');
                                return false;
                            }
                            if (0 < params.height && params.height != img.height) {
                                layer.msg('图片高度必须为' + params.width + 'px');
                                return false;
                            }
                            if (params.scale && params.scale.length == 2 && params.scale[0] / params.scale[1] != img.width / img.height) {
                                layer.msg('图片宽和高的比例为:' + params.scale[0] + " : " + params.scale[1]);
                                return false;
                            }
                            obj.upload(index, file);
                        };
                        img.src = result;
                    });
                }
            }
        }
        var result = $.extend({}, up.defaultParams, params);
        result.data = {fileType: result.accept, fileSize: result.size}
        if (typeof result.done != 'function') {
            result.done = function (res, index, upload) {
                layer.closeAll('loading'); //关闭loading
                $.messager.alert0(res);
                if (res.success) {
                    //设置文件路径
                    var item = this.item;
                    $(item).parents().find("img.image-path").attr("src", result.imageBaseUrl + res.data);
                    $(item).parents().find("input.image-input").val(res.data);
                    $(item).css("cssText", "color:transparent!important;background: url(" + result.imageBaseUrl + res.data + ") no-repeat  center");
                }
            }
        }

        layui.use('upload', function () {
            var upload = layui.upload;
            //执行实例
            var uploadInst = upload.render(result);
        });
    }
    up.uploadImageFile = function (params) {
        if (!params) {
            params = {};
        }
        params.accept = "images";
        params.exts = "jpg|png|gif|bmp|jpeg";
        up.uploadFile(params);

    }


    
    exports('uploader', up);
});

